package sunshine.sun.cn.notifidemo;

import android.media.MediaPlayer;

import java.io.IOException;

/**
 * Create by sunfulin
 * Time: 2020/6/4 11:45
 */
public class MediaUtils {
    private static final MediaUtils ourInstance = new MediaUtils();

    static MediaUtils getInstance() {
        return ourInstance;
    }

    private static MediaPlayer mediaPlayer;

    private MediaUtils() {
        mediaPlayer = new MediaPlayer();
    }

    public void playDataSource(String rs) throws IOException {
        if (mediaPlayer == null) {
            return;
        }
//        if (mediaPlayer.isPlaying()) {
//            mediaPlayer.reset();//重置为初始状态
//        }
        mediaPlayer.reset();//重置为初始状态
        setDataSource(rs);
        prepare();
        play();
    }

    public void setDataSource(String rs) throws IOException {
        mediaPlayer.setDataSource(rs);
    }

    public void prepare() throws IOException {
        mediaPlayer.prepare();//准备播放
    }

    public void play() {
        mediaPlayer.start();//播放
    }
}
